<?php

/**
 * This is the model class for table "member".
 *
 * The followings are the available columns in table 'member':
 * @property string $member_id
 * @property string $firstname
 * @property string $lastname
 * @property string $city
 * @property string $address
 * @property string $end_of_substriction
 * @property string $date_created
 * @property string $date_updated
 *
 * The followings are the available model relations:
 * @property Land[] $lands
 * @property Book[] $books
 */
class BaseMember extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Member the static model class
     */
    public static function model($className = 'Member') {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'member';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('firstname, lastname, city, address', 'required'),
            array('date_created, date_updated', 'safe'),
            array('firstname, lastname, city, address', 'length', 'max' => 64),
            array('end_of_substriction', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('member_id, firstname, lastname, city, address, end_of_substriction, date_created, date_updated', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'lands' => array(self::HAS_MANY, 'Land', 'member_id'),
            'books' => array(self::MANY_MANY, 'Book', 'land(book_id, member_id)'),
            'avgLandDays' => array(self::STAT, 'Land', 'member_id',
                'select' => 'truncate(avg(datediff(end,start)),2)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'member_id' => 'Member',
            'name' => 'Név',
            'firstname' => 'Keresztnév',
            'lastname' => 'Vezetéknév',
            'city' => 'Város',
            'address' => 'Cím',
            'end_of_substriction' => 'Tagság vége',
            'date_created' => 'Létrehozás dátuma',
            'date_updated' => 'Módosítás dátuma',
            'avgLandDays' => 'Kölcsönzési napok átlaga',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('member_id', $this->member_id, true);
        $criteria->compare('firstname', $this->firstname, true);
        $criteria->compare('lastname', $this->lastname, true);
        $criteria->compare('city', $this->city, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('end_of_substriction', $this->end_of_substriction, true);
        $criteria->compare('date_created', $this->date_created, true);
        $criteria->compare('date_updated', $this->date_updated, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

}